<template>
	<view class="container">
		<view class="box">
			<view class="title">线上版本</view>
			<view class="name">版本号：{{ releaseInfo.release_version }}</view>
			<view class="name">提交时间：{{ formatDate(releaseInfo.release_time) }}</view>
			<view class="name">版本描述：{{ releaseInfo.release_desc }}</view>
			<view class="btn">当前版本</view>
		</view>
		<view class="box">
			<view class="title">已选择版本</view>
			<view class="name">版本号：{{ expInfo.exp_version }}</view>
			<view class="name">提交时间：{{ formatDate(expInfo.exp_time) }}</view>
			<view class="name">版本描述：{{ expInfo.exp_desc }}</view>
			<view
				class="btn"
				style="background-color: #165dff"
				v-if="releaseInfo.release_version != expInfo.exp_version"
				@click="submitAudit"
			>
				提交审核
			</view>
		</view>
		<view class="box">
			<view class="title">正在审核版本</view>
			<view class="name">版本号：{{ auditInfo.user_version }}</view>
			<view class="name">提交审核时间：{{ formatDate(auditInfo.submit_audit_time) }}</view>
			<view class="name">版本描述：{{ auditInfo.user_desc }}</view>
			<view class="name">
				审核状态：
				<text v-if="auditInfo.status == 0" style="color: #67c23a">审核成功</text>
				<text v-else-if="auditInfo.status == 1">审核被拒绝</text>
				<text v-else-if="auditInfo.status == 2">审核中</text>
				<text v-else-if="auditInfo.status == 3">已撤回</text>
				<text v-else-if="auditInfo.status == 4">审核延后</text>
			</view>
			<view class="name" v-if="auditInfo.status == 1">原因描述：{{ auditInfo.reason }}</view>
			<view class="btn" v-if="auditInfo.status == 0&&releaseInfo.release_version != auditInfo.user_version" style="background-color: #165dff" @click="getProgram">
				发布代码
			</view>
			<view class="btn" v-if="auditInfo.status == 2" style="background-color: red" @click="getUndoAudit">
				撤回审核
			</view>
		</view>

		<view class="box">
			<view class="title">模板库最新版本</view>
			<view class="title">{{ modelDetail.title }}</view>
			<view class="name">版本号：{{ templateInfo.user_version }}</view>
			<view class="name">更新时间：{{ formatDate(templateInfo.create_time) }}</view>
			<view
				class="btn"
				style="background-color: #165dff"
				@click="setTemp"
				v-if="templateInfo.user_version != expInfo.exp_version"
			>
				选择最新版本
			</view>
			<view
				class="btn"
				v-else
			>
				已是最新版本
			</view>
		</view>
	</view>
</template>

<script>
import {
	getUserAuthorizeApplet,
	getUserTemplateConf,
	getAppletOkSetCategoryName,
	getAppletVersionInfo,
	getLatestAuditStatus,
	submitAudit,
	releaseProgram,
	undoAudit,
	getTemplateList,
	commitProgramCode
} from '../../../utils/api.js';
export default {
	data() {
		return {
			appId: '',
			modelDetail: {},
			releaseInfo: {},
			expInfo: {},
			auditInfo: {},
			templateList: [],
			templateInfo: {},
			categoryList: []
		};
	},
	onLoad() {
		this.getMymodel();
		this.getAppid();
	},
	onShow() {},
	methods: {
		formatDate(cellValue) {
			if (cellValue == null || cellValue == '') return '';
			let date = new Date(parseInt(cellValue) * 1000); //时间戳为10位需*1000，如果为13位的话不需乘1000。
			let Y = date.getFullYear() + '-';
			let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
			let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
			let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
			let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
			let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
			return Y + M + D + h + m + s;
		},
		// 获取appid
		getAppid() {
			getUserAuthorizeApplet()
				.then((res) => {
					if (res.code == 200) {
						this.appId = res.body.appId;
					}
				})
				.then(() => {
					this.getappVresion();
					this.getAuditInfo();
					this.getCategoryName();
					// getCategoryName()
					// getAuditStatus()
				});
		},
		// 获取全部模版
		getMymodel() {
			let data = {
				userServiceToken: uni.getStorageSync('userInfo').userServiceToken
			};
			getUserTemplateConf(data).then((res) => {
				if (res.code == 200) {
					this.modelDetail = res.body;
					this.getTemplateList();
				}
			});
		},
		// 获取类目
		getCategoryName() {
			const data = {
				appId: this.appId
			};
			getAppletOkSetCategoryName(data).then((res) => {
				if (res.code == 200) {
					this.categoryList = res.body?.category_list || [];
				}
			});
		},
		// 线上版本
		getappVresion() {
			let data = {
				appId: this.appId
			};
			getAppletVersionInfo(data).then((res) => {
				if (res.code == 200) {
					this.releaseInfo = res.body.release_info;
					this.expInfo = res.body.exp_info;
				}
			});
		},
		// 正在审核
		getAuditInfo() {
			let data = {
				appId: this.appId
			};
			getLatestAuditStatus(data).then((res) => {
				if (res.code == 200) {
					this.auditInfo = res.body;
				}
			});
		},
		// 选择最新版本
		setTemp() {
			uni.showLoading({
				title: '选择中...',
				mask: true
			});
			let data = {
				appId: this.appId,
				templateId: this.templateInfo.template_id,
				extJson: {
					extEnable: true,
					extAppid: this.appId,
					directCommit: true,
					ext: {
						appid: this.appId
					}
				},
				userVersion: this.templateInfo.user_version,
				userDesc: this.templateInfo.user_desc
			};
			commitProgramCode(data).then((res) => {
				if (res.code == 200) {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					});

					this.getappVresion();
					this.getAuditInfo();
				}
			});
		},
		// 提交审核
		submitAudit() {
			if(this.expInfo.exp_version==this.auditInfo.user_version){
				if(this.auditInfo.status==0){
					uni.showToast({
						title: '当前代码审核已通过，请发布',
						icon:'none'
					});
					return
				}else if(this.auditInfo.status==2){
					uni.showToast({
						title: '当前代码正在审核中，请等待',
						icon:'none'
					});
					return
				}
				
				uni.showLoading({
					title: '提交审核中...',
					mask: true
				});
				
				let itemList = [];
				this.categoryList.forEach((item) => {
					itemList.push({
						first_class: item.first_class,
						first_id: item.first_id,
						second_class: item.second_class,
						second_id: item.second_id,
						third_class: item.third_class || '',
						third_id: item.third_id || ''
					});
				});
				const data = {
					appId: this.appId,
					item_list: itemList,
					order_path: 'pages/order/order'
				};
				submitAudit(data).then((res) => {
					if (res.code == 200) {
						uni.hideLoading();
						uni.showToast({
							title: res.msg
						});
						this.getappVresion();
						this.getAuditInfo();
					}
				});
			}else{
				uni.showLoading({
					title: '提交审核中...',
					mask: true
				});
				
				let itemList = [];
				this.categoryList.forEach((item) => {
					itemList.push({
						first_class: item.first_class,
						first_id: item.first_id,
						second_class: item.second_class,
						second_id: item.second_id,
						third_class: item.third_class || '',
						third_id: item.third_id || ''
					});
				});
				const data = {
					appId: this.appId,
					item_list: itemList,
					order_path: 'pages/order/order'
				};
				submitAudit(data).then((res) => {
					if (res.code == 200) {
						uni.hideLoading();
						uni.showToast({
							title: res.msg
						});
						this.getappVresion();
						this.getAuditInfo();
					}
				});
			}
			
			
			
		},
		// 发布代码
		getProgram() {
			uni.showLoading({
				title: '发布代码中...',
				mask: true
			});
			const data = {
				appId: this.appId
			};
			releaseProgram(data).then((res) => {
				if (res.code == 200) {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					});
					this.getappVresion();
					this.getAuditInfo();
				}
			});
		},
		// 撤回审核
		getUndoAudit() {
			uni.showLoading({
				title: '撤回审核中...',
				mask: true
			});
			const data = {
				appId: this.appId
			};
			undoAudit(data).then((res) => {
				if (res.code == 200) {
					uni.hideLoading();
					uni.showToast({
						title: res.msg
					});
					this.getappVresion();
					this.getAuditInfo();
				}
			});
		},

		// 模板库全部版本
		getTemplateList() {
			getTemplateList().then((res) => {
				if (res.code == 200) {
					res.body.template_list.forEach((item) => {
						if (item.user_desc.indexOf('DC' + this.modelDetail.id) != -1) {
							this.templateList.push(item);
						}
					});

					this.templateList.sort(function (a, b) {
						return b.draft_id - a.draft_id;
					});
					this.templateInfo = this.templateList[0];
					// for(var i=0; i<this.templateList.length-1; i++){
					// 	//每一轮比较要比多少次
					// 	for(var j=0; j<this.templateList.length-1-i; j++){
					// 	    //如果第一个比第二个大，就交换他们两个位置
					// 	    if(this.templateList[j].draft_id<this.templateList[j+1].draft_id){
					// 	        var temp = this.templateList[j].draft_id;
					// 	        this.templateList[j].draft_id = this.templateList[j+1].draft_id;
					// 	        this.templateList[j+1].draft_id = temp;
					// 	    }
					// 	}
					// }
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f5f5f5;
}
.container {
	padding: 20rpx;
}
.box {
	padding: 30rpx 20rpx;
	border-radius: 20rpx;
	background-color: #fff;
	position: relative;
	margin-bottom: 30rpx;
	.title {
		font-family: Source Han Sans;
		font-size: 28rpx;
		font-weight: 700;
		margin-bottom: 30rpx;
	}
	.name {
		font-family: Source Han Sans;
		font-size: 28rpx;
		margin-top: 10rpx;
		color: #86909c;
	}
	.btn {
		height: 56rpx;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 20rpx;
		color: #fff;
		background-color: #5ac37c;
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		font-family: Source Han Sans;
		font-size: 28rpx;
	}
}
</style>
